package com.newhopegroup.useractiontrack.core.authentication.dubbo;

import com.newhopegroup.useractiontrack.api.exception.TrackApiException;
import com.newhopegroup.useractiontrack.api.userAction.interf.UserActionInterf;
import com.newhopegroup.useractiontrack.api.userAction.vo.UserActionResultVo;
import com.newhopegroup.useractiontrack.api.userAction.vo.UserActionVo;
import com.newhopegroup.useractiontrack.core.authentication.dao.UserActionDao;
import com.newhopegroup.useractiontrack.core.authentication.service.UserActionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by liusen on 15-7-5.
 */
@Service("UserActionInterfImplService")
public class UserActionInterfImpl implements UserActionInterf {

    private final Logger logger = LoggerFactory.getLogger(UserActionInterfImpl.class);

    @Resource
    private UserActionDao userActionDao;

    @Resource
    private UserActionService userActionService;

    @Override
    public void addUserActionVo(UserActionVo userActionVo) throws TrackApiException {

        logger.info("userActionVo:[{}]", userActionVo);

        try {
            if (userActionVo == null) {
                throw new TrackApiException("用户行为对象UserActionVo为空！！");
            }
            userActionDao.addUserAction(userActionVo);
        } catch (TrackApiException e) {
            logger.error(e.getMessage(), e);
            throw new TrackApiException(e.getMessage(), e);

        }
    }

    @Override
    public List<UserActionVo> queryByUuid(String uuid) throws TrackApiException {
        logger.info("uuid:[{}]", uuid);

        try {
            if (uuid == null) {
                throw new TrackApiException("用户唯一表示UUID为空！！");
            }
            return userActionDao.queryByUuid(uuid);
        } catch (TrackApiException e) {
            logger.error(e.getMessage(), e);
            throw new TrackApiException(e.getMessage(), e);

        }
    }

    @Override
    public List<UserActionVo> queryByDate(Date startDate, Date endDate) throws TrackApiException {
        logger.info("startDate,endDate:[{}]", startDate, endDate);

        try {
            if (startDate == null || endDate == null) {
                throw new TrackApiException("查询时间不能为空！！");
            }
            return userActionDao.queryByDate(startDate, endDate);
        } catch (TrackApiException e) {
            logger.error(e.getMessage(), e);
            throw new TrackApiException(e.getMessage(), e);

        }
    }

    @Override
    public List<UserActionResultVo> querybydate(Date startDate, Date endDate) throws TrackApiException {
        logger.info("startDate,endDate:[{}]", startDate, endDate);

        try {
            if (startDate == null || endDate == null) {
                throw new TrackApiException("查询时间不能为空！！");
            }
            return userActionService.querybydate(startDate, endDate);
        } catch (TrackApiException e) {
            logger.error(e.getMessage(), e);
            throw new TrackApiException(e.getMessage(), e);

        }
    }
}
